//
//  AdSettingViewController.m
//  ESPMobile
//
//  Created by fly on 14/12/22.
//  Copyright (c) 2014年 Javor. All rights reserved.
//

#import "AdSettingViewController.h"
#import "AdSettingView.h"
#import "OptionSettingModel.h"

//#import<IOSESPBaseFramework/PlistUtil.h>
#import "PlistUtil.h"

@interface AdSettingViewController ()<UITextFieldDelegate,UIAlertViewDelegate,AdSettingViewDelegate>

@property (nonatomic,weak) AdSettingView  *setView;

@property(nonatomic,strong) NSMutableArray *textFields;

@property (nonatomic,strong) OptionSettingModel *model;

@property(nonatomic,strong) NSMutableDictionary *settingDict;

@property(nonatomic,strong) NSString *setPlistPath;
@end

@implementation AdSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AdSettingView *setView = [[AdSettingView alloc] init];
    setView.delegate = self;
    self.view= setView;
    self.setView = setView;
    //从Plist中加载数据
    _setPlistPath = [PlistUtil getPathWithName:@"LOCAL_RESOURCE/PLISTE/OptionSetting.plist"];
    
    //NSLog(@"%@",_setPlistPath);
     _settingDict = [[NSMutableDictionary dictionaryWithContentsOfFile:_setPlistPath] mutableCopy];
    
    self.model  = [[OptionSettingModel alloc] initWithDict:_settingDict];
    
    _setView.textAddress.tag = 1;
    _setView.textAddress.text = _model.ip;
    _setView.textProt.tag = 2;
    _setView.textProt.text = _model.port;
    _setView.textPath.tag = 3;
    _setView.textPath.text = _model.server;
    _setView.textName.tag = 4;
    _setView.textName.text = _model.product;
    _setView.textData.tag = 5;
    _setView.textData.text = _model.DBName;
    _setView.textMark.tag = 6;
    _setView.textMark.text = _model.DBNO;
//    _setView.isAd
    if([_model.isAd  isEqual:@"1"])
    {
        [_setView.isAd setOn:YES];
    }
    else
    {
        [_setView.isAd setOn:NO];
    }
    
    
    _setView.textAddress.delegate = self;
    _setView.textProt.delegate = self;
    _setView.textPath.delegate = self;
    _setView.textName.delegate = self;
    _setView.textData.delegate = self;
    _setView.textMark.delegate = self;
    _setView.textAddress.returnKeyType = UIReturnKeyNext;
    _setView.textProt.returnKeyType = UIReturnKeyNext;
    _setView.textPath.returnKeyType = UIReturnKeyNext;
    _setView.textName.returnKeyType = UIReturnKeyNext;
    _setView.textData.returnKeyType = UIReturnKeyNext;
    _setView.textMark.returnKeyType = UIReturnKeyDone;
    
    self.textFields = [NSMutableArray new];
    
    [self.textFields addObject:_setView.textAddress];
    [self.textFields addObject:_setView.textProt];
    [self.textFields addObject:_setView.textPath];
    [self.textFields addObject:_setView.textName];
    [self.textFields addObject:_setView.textData];
    [self.textFields addObject:_setView.textMark];
    
    for (UITextField *textFiled in self.textFields) {
        [textFiled addTarget:self action:@selector(beginEdit:) forControlEvents:UIControlEventEditingDidBegin];
    }
    

    

}

//开始编辑
-(void) beginEdit:(UITextField *)filed{
    if([UIScreen mainScreen].bounds.size.height>700) return ;
    CGPoint point=self.setView.scrollView.contentOffset;
    if(filed.tag ==1||filed.tag ==2) return;
    
    point.y = filed.tag*30;
    [UIView animateWithDuration:0.2 animations:^{
        self.setView.scrollView.contentOffset = point;
    }];
}


-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden   = NO;
    
}

//监听键盘return按钮
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField.tag>=6)
    {
        [self doSave];
        return YES;
    }
    
    UITextField *temp = self.textFields[textField.tag-1];
    [temp resignFirstResponder];
    [self.textFields[textField.tag] becomeFirstResponder];
    return YES;
}

//点击保存执行doSave
-(void)doSave{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"设置" message:@"是否确定保存？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"保存", nil];
    NSLog(@"Save");
    [alertView show];
}

#pragma AlertView代理
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex!=1) return;
    _model.ip = _setView.textAddress.text ;
    _model.port = _setView.textProt.text;
    _model.server = _setView.textPath.text;
    _model.product =_setView.textName.text;
    _model.DBName = _setView.textData.text;
    _model.DBNO = _setView.textMark.text ;
    
    if(_setView.isAd.isOn)
    {
        _model.isAd = @"1";
    }
    else
    {
        _model.isAd = @"0";
    }
    
    [self.settingDict setObject:_model.server forKey:@"server"];
    [self.settingDict setObject:_model.port forKey:@"port"];
    [self.settingDict setObject:_model.product forKey:@"product"];
    [self.settingDict setObject:_model.DBName forKey:@"DBName"];
    [self.settingDict setObject:_model.isSafe forKey:@"isSafe"];
    [self.settingDict setObject:_model.isAd forKey:@"isAd"];
    [self.settingDict setObject:_model.DBNO forKey:@"DBNO"];
    [self.settingDict setObject:_model.ip forKey:@"ip"];
    
    
    if([_settingDict writeToFile:_setPlistPath atomically:YES])
    {
        NSLog(@"保存成功");
    }
    else
    {
        NSLog(@"保存失败");
    }
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
